package com.spa.domain.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spa.domain.entity.Coupon;
import com.spa.infrastructure.mapper.CouponMapperStruct;
import com.spa.infrastructure.repository.condition.CouponCondition;
import com.spa.infrastructure.repository.po.CouponPO;
import com.spa.infrastructure.repository.service.CouponRepositoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 团购券  domain
 *
 */
@Service
public class CouponService {

    @Resource
    private CouponRepositoryService couponRepositoryService;

    public boolean existByUserIdAndServiceId(long userId, Long serviceId) {
        CouponCondition condition = new CouponCondition();
        condition.setUserId(userId);
        condition.setServiceId(serviceId);
        condition.setStatus(0);
        return couponRepositoryService.exist(condition);
    }

    public long countByUser(long userId) {
        CouponCondition condition = new CouponCondition();
        condition.setUserId(userId);
        condition.setStatus(0);
        return couponRepositoryService.countByCondition(condition);
    }

    public long countPerDay(String beginTime, String endTime) {
        CouponCondition condition = new CouponCondition();
        condition.setStatus(1);
        condition.setUseBeginTime(beginTime);
        condition.setUseEndTime(endTime);
        return couponRepositoryService.countByCondition(condition);
    }

    public int insertCoupon(CouponPO po) {
        return couponRepositoryService.insert(po);
    }

    public void useCoupon(long id, long orderId) {
        CouponPO po = new CouponPO();
        po.setId(id);
        po.setStatus(1);
        po.setOrderId(orderId);
        po.setUseTime(new Date());
        couponRepositoryService.updateByCondition(po);
    }

    public void returnCoupon(long id) {
        CouponPO po = new CouponPO();
        po.setId(id);
        po.setStatus(0);
        po.setOrderId(0L);
        couponRepositoryService.updateByCondition(po);
    }

    public Page<Coupon> page(CouponCondition condition) {
        return CouponMapperStruct.INSTANCE.po2Entity4Page(couponRepositoryService.page(condition));
    }

    public List<Coupon> list(CouponCondition condition) {
        return CouponMapperStruct.INSTANCE.po2Entity4List(couponRepositoryService.listAll(condition));
    }

    public List<Coupon> getCouponsByUserIdAndServiceId(long userId, Long serviceId) {
        CouponCondition condition = new CouponCondition();
        condition.setUserId(userId);
        condition.setServiceId(serviceId);
        condition.setStatus(0);//未使用
        List<CouponPO> couponPOList = couponRepositoryService.listAll(condition);
        return CouponMapperStruct.INSTANCE.po2Entity4List(couponPOList);
    }

    public List<Coupon> getCouponsByOrderId(long orderId) {
        CouponCondition condition = new CouponCondition();
        condition.setOrderId(orderId);
        List<CouponPO> couponPOList = couponRepositoryService.listAll(condition);
        return CouponMapperStruct.INSTANCE.po2Entity4List(couponPOList);
    }

}
